<template>
  <view class="container">
    <medical-goods page="life" :medicalList="list" pageType="goods"></medical-goods>
    <emptyData
      :width="empty.width"
      :height="empty.height"
      :text="empty.text"
      :icon="empty.icon"
      :marginTop="empty.marginTop"
      v-if="list.length == 0"
    ></emptyData>
  </view>
</template>

<script>
import medicalGoods from "../../components/medicalGoods/medicalGoods.vue";
export default {
  components: {
    medicalGoods,
  },
  data() {
    return {
      page: 1,
      list: [],
      empty: {
        width: 135,
        height: 134,
        text: "暂无数据",
        icon: "/image/emptyAddress.png",
        marginTop: 100,
      },
      category_id: 0,
    };
  },
  onLoad(e) {
    this.isIphoneX = this.$util.uniappIsIPhoneX();
    if (e.category_id) {
      this.category_id = e.category_id;
    }

    if (e.category_name) {
      uni.setNavigationBarTitle({
        title: e.category_name,
      });
    }

    this.getList();
  },
  onReachBottom() {
    this.page++;
    this.getList();
  },
  methods: {
    getList() {
      this.$api.sendRequest({
        url: "/api/life/goodslist",
        data: {
          page: this.page,
          page_size: 20,
          category_id: this.category_id,
        },
        success: (res) => {
          if (res.code == 0) {
            if (this.page == 1) this.list = [];
            this.list = [...this.list, ...res.data.list];

            this.list.forEach((item) => {
              if (item.goods_label) {
                // 判断里面是否有逗号，如果有就分割成数组
                if (item.goods_label.indexOf(",") != -1) item.goods_label = item.goods_label.split(",");
                else item.goods_label = [item.goods_label];
              } else item.goods_label = [];

              if (item.goods_image) {
                var images = item.goods_image.split(",");
                item.goods_image = images[0];
              }
            });
          }
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
